package com.zhuiyi.yiqingbackend.service.impl;

import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.domain.Notice;
import com.zhuiyi.yiqingbackend.domain.response.NoticeRespose;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.NoticeMapper;
import com.zhuiyi.yiqingbackend.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.logging.log4j.ThreadContext.isEmpty;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Override
    public List<Notice> getNotices() {
        List<Notice> notices = noticeMapper.getNotices();
        return notices;
    }

    @Override
    public NoticeRespose getNotices2() {
        List<Notice> first = new ArrayList<>();
        List<Notice> second = new ArrayList<>();
        List<Notice> third = new ArrayList<>();
        for (Notice notice : noticeMapper.getNotices()) {
            String level = notice.getLevel();
            if(StringUtils.equals(level, "国家级")) {
                first.add(notice);
            }
            if(StringUtils.equals(level, "都市级")) {
                second.add(notice);
            }
            if(StringUtils.equals(level, "社区级")) {
                third.add(notice);
            }
        }
        NoticeRespose noticeRespose = new NoticeRespose(first, second, third);

        return noticeRespose;
    }

    @Override
    public boolean addNotice(Notice notice) {
        Date time = notice.getTime();
        String message = notice.getMessage();
        String title = notice.getTitle();
        if(time==null || time.equals("")){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"发布时间为空");
        }
        if(StringUtils.isAnyBlank(message,title)){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"标题或内容为空");
        }
        return noticeMapper.addNotice(notice);
    }

    @Override
    public boolean deleteNotice(int id) {
        Notice notice = noticeMapper.getNoticeById(id);
        if(notice == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "删除失败");
        }
        return noticeMapper.deleteNotice(id);
    }

    @Override
    public boolean updateNotice(Notice notice) {
        Notice search = noticeMapper.getNoticeById(notice.getId());
        if(search == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "更新失败,没有该公告");
        }
        return noticeMapper.updateNotice(notice);
    }

    @Override
    public List<Notice> getNoticeById(int id) {
        if(id <= 0 || StringUtils.isAnyBlank(Integer.toString(id))){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "查询失败,没有该公告");
        }
        Notice notice = noticeMapper.getNoticeById(id);
        if(notice == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "该公告不存在");
        }
        List<Notice> list = new ArrayList<>();
        list.add(notice);
        return list;
    }
}
